<template>
	<view>
		<zy-select :selectList="selectList" :zy_customCss="zy_customCss" @lineHandler="lineHandler"
			class="select"></zy-select>
		<zy-table :headList="headList" :dataList="dataList" lineClickShow @lineClick="lineClick" border></zy-table>
	</view>
</template>
<script>
	import {
		findReasonType,
		callReasonQueryList,
		saveBill
	} from "@/api/homepage.js"
	export default {
		props: ['parentValue'],
		data() {
			return {
				endIcon: 'iconfont icon-xiangxia',
				selectList: [],
				zy_customCss: {
					borderRadius: "50rpx",
					border: "5rpx solid #409EFF",
				}, //下拉框自定义样式
				headList: [{
						name: this.$t('homepage.TerminalCall.callCode'),
						key: 'reasonCode',
					},
					{
						name: this.$t('homepage.TerminalCall.callType'),
						key: 'reasonType',
					}, {
						name: this.$t('homepage.TerminalCall.callRemark'),
						key: 'remarks',
					},
				],
				dataList: [],
			}
		},
		created() {
			this.getSelectList()
		},
		methods: {
			// 获取下拉数据
			getSelectList() {
				findReasonType().then((res) => {
					if (res.code == 0) {
						res.data.forEach((item, index) => {
							let obj = {}
							obj.value = index
							obj.label = item
							this.selectList.push(obj)
						})
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});

			},
			//搜索下拉框内容查询
			lineHandler(e) {
				this.dataList = []
				callReasonQueryList({
					reasonType: e.item.label
				}).then((res) => {
					if (res.code == 0) {
						this.dataList = res.data
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			// 选择行
			lineClick(e) {
				uni.showModal({
					title: this.$t('homepage.TerminalCall.terminalCall'),
					content: this.$t('public.isSure') + this.$t('homepage.TerminalCall.callCode') + this.$t('public.colon') + e.value
						.reasonCode,
					success: function(res) {
						if (res.confirm) {
							saveBill({
								callReasonId: e.value.id,
								reasonType: e.value.reasonType
							}).then((res) => {
								if (res.code == 0) {
									uni.showToast({
										title: res.message,
										icon: "none",
										duration: 3000
									})
									setTimeout(() => {
										uni.redirectTo({
											url: "/pages/HomePage/TerminalCall/index"
										})
									}, 500)
								} else {
									uni.showToast({
										title: res.message,
										icon: "none",
										duration: 3000
									})
								}
							}).catch((res) => {
								uni.showToast({
									title: res.message,
									icon: "none",
									duration: 3000
								})
							})
						} else {
							uni.showToast({
								title: this.$t('homepage.TerminalCall.cancelCall'),
								icon: "none",
								duration: 3000
							})
						}
					}
				})
			},
		}
	}
</script>
<style lang="scss" scoped>
	.select {
		margin-top: 5px;
		margin-bottom: 10px;
	}
</style>